package cn.tedu.tmall.admin.mall.dao.persist.repository.impl;

import cn.tedu.tmall.admin.mall.dao.persist.mapper.GoodMapper;
import cn.tedu.tmall.admin.mall.dao.persist.repository.IGoodRepository;
import cn.tedu.tmall.admin.mall.pojo.entity.Goods;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodRepositoryImpl implements IGoodRepository {
    @Autowired
    private GoodMapper mapper;
    @Override
    public int insert(Goods goods) {
        return mapper.insert(goods);
    }

    @Override
    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public int updateById(Goods goods) {
        return mapper.updateById(goods);
    }

    @Override
    public GoodsStandardVO selectById(Long id) {
        GoodsStandardVO goodsStandardVO = mapper.selectById(id);
        return goodsStandardVO;
    }

    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsListItemVO> goodsListItemVOS = mapper.list();
        PageInfo<GoodsListItemVO> pageInfo = new PageInfo<>(goodsListItemVOS);
        PageData<GoodsListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        return pageData;
    }

    @Override
    public PageData<GoodsSearchPO> listSearch(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsSearchPO> searchPOList = mapper.listBatch();
        PageInfo<GoodsSearchPO> pageInfo = new PageInfo<>(searchPOList);
        PageData<GoodsSearchPO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> listByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsListItemVO> goodsListItemVOS = mapper.listByCategoryId(categoryId);
        PageInfo<GoodsListItemVO> pageInfo = new PageInfo<>(goodsListItemVOS);
        PageData<GoodsListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        return pageData;
    }
}
